package com.ds.renderer;

import com.ds.theme.ColorTheme;
import com.ds.theme.LightBlueColorTheme;
import com.ds.components.JXButton;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Arc2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
/* Copyright (C) 2005, 2006 by Daniel Gheorghian
 All rights reserved
 
 Use and distribute freely, except: don't remove my name from the source or documentation 
 (don't take credit for my work), mark your changes (don't get me blamed for your possible bugs),
 don't alter or remove this notice.
 
 No warrantee of any kind, express or implied, is included with this software; use at your
 own risk, responsibility for damages (if any) to anyone resulting from the use of this
 software rests entirely with the user.
 
 Send bug reports, bug fixes, enhancements, requests, flames, etc., and I'll try to keep 
 a version up to date.  I can be reached at: daniel.gheorghian@gmail.com
*/

public class JXButtonRenderer implements GenericRenderer
{
	private GeneralPath shape;
	private ColorTheme colors;
	private int height;
	private int width;
	private boolean random;

	public JXButtonRenderer()
	{
		this( new LightBlueColorTheme() );
	}

	public JXButtonRenderer( ColorTheme colors )
	{
		this.colors = colors;

		if( Math.random() < 0.5 )
		{
			random = true;
		}
		else
		{
			random = false;
		}
	}

	public void render( Graphics g, JComponent c, boolean disabled, boolean isSelected, boolean flash )
	{
		update( c.getWidth(), c.getHeight() );

		Graphics2D g2D = ( Graphics2D ) g;
		g2D.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );

		g2D.setColor( Color.black );
		g2D.fillRect( 0, 0, width, height );

		g2D.setPaint( colors.getBackgroundPaint() );
		g2D.fill( shape );

		g2D.setColor( Color.black );
		g2D.setStroke( new BasicStroke( 5.0f ) );
		g2D.drawLine( 30, 4, 30, height - 8 );

		if( isSelected )
		{
			g2D.setPaint( colors.getForegroundSelectedPaint() );
		}
		else
		{
			g2D.setPaint( colors.getForegroundPaint() );
		}

		g2D.setFont( new Font( "Arial", Font.BOLD, 13 ) );

		if( isSelected )
		{
			g2D.setPaint( colors.getForegroundSelectedPaint() );
		}
		else
		{
			g2D.setPaint( colors.getForegroundPaint() );
		}

		paintText( g2D, ((JXButton)c).getText() );
	}

	private void update( int width, int height )
	{
		if( this.height != height || this.width != width )
		{
			this.height = height;
			this.width = width;
			this.shape = new GeneralPath();

			if( !random )
			{
				Arc2D arc = new Arc2D.Double( new Rectangle( 1, 4, 28, height - 11 ), 90.0, 180.0, Arc2D.OPEN );
				shape.append( arc, false );
				shape.lineTo( width - 1, height - 7 );
				shape.lineTo( width - 1, 4 );
				shape.lineTo( 14, 4 );
			}
			else
			{
//				shape.append( new Line2D.Double( 1, 4, 28, height - 11 ), false );
				shape.append( new Rectangle2D.Double( 28, 4, width - 29, height - 11 ), false );
			}

			this.colors.update( new Dimension( this.width, this.height ) );
		}
	}

	private void paintText( Graphics2D g2D, String text )
	{
		if( g2D.getFontMetrics().stringWidth( text ) < width - 50 )
		{
			g2D.drawString( text, 40, 30 );
		}
		else
		{
			String str = text.substring( 0, 0 );
			int n = g2D.getFontMetrics().stringWidth( "..." ), i;
			int w = width - 60 - n;

			i = 1;
			while( g2D.getFontMetrics().stringWidth( str ) < w && i < text.length() )
			{
				str += text.substring( i - 1, i );
				i++;
			}
			g2D.drawString( str + "...", 40, 30 );
		}
	}}
